package com.example.producer;

import com.example.constant.RedisConstant;
import com.example.util.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/5/26 17:40
 * @Description TODO
 */
@Component
@Slf4j
public class ProducerMessage {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 定时任务。没隔一分钟执行一次
     */
    @Scheduled(fixedRate = 1000 * 10)
    @Async(value = "forkJoinPool")
    public void makeData() {
        StreamOperations<String, String, String> streamOperations = RedisUtil.getStringStringStringStreamOperations(redisTemplate);

        Map<String, String> map = new HashMap<>();
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        map.put("id",uuid);
        MapRecord<String, String, String> mapRecord = MapRecord.create(RedisConstant.COMMENT_KEY,map);

        for (int i = 0; i < 10; i++) {
            RecordId recordId1 = streamOperations.add(mapRecord);
            RecordId recordId2 = streamOperations.add(RedisConstant.COMMENT_KEY,map);
        }

    }
}
